/////////////////////////////////////////////
// ObjectARX defined commands

#include "StdAfx.h"
#include "StdArx.h"


// This is command 'WINDO'
void windo()
{
	AcDbBlockTableRecord *pBlkTableRecord;

	RectWindow *pRectWindow;	// RectWindow class

	pRectWindow = new RectWindow;
	
	if(!getWindowInputParameters(pRectWindow))
	{
		acutPrintf("\nError in window input parameters. ");
		if(pRectWindow != NULL)
		{
			delete pRectWindow;
		}
		return;
	}

	if(!getModelSpaceRecord(pBlkTableRecord))
	{
		return;
	}

	pRectWindow->drawWindow(pBlkTableRecord);

	pBlkTableRecord->close();

	pRectWindow->addWindowInfo(pRectWindow);

	pRectWindow->close();
}

// This is command 'PWI'
void pickWindowInfo()
{
	AcDbDatabase *pCurDb;
	AcDbObject *pObj;
	RectWindow *pRectWindow = NULL;
	AcDbDictionary *pNamedObjD, *pDict;

	AcDbObjectId entId, objId;

	AcGePoint2d sp;

	AcDbHandle handEnt;

	ads_name ename;
	ads_point pickpt;
	char strHandle[17];
	int rc;

	pCurDb = acdbHostApplicationServices()->workingDatabase();

	// Get the Nameed Object Dictionary
	pCurDb->getNamedObjectsDictionary(pNamedObjD, AcDb::kForRead);

	if(!pNamedObjD->has("WINDO_INFO"))
	{
		acutPrintf("\nNo WINDO_INFO object dictionary");
		pNamedObjD->close();
		return;
	}

	pNamedObjD->getAt("WINDO_INFO", (AcDbObject*&)pDict, AcDb::kForRead);
	pNamedObjD->close();

	// Select the Window entity to get its handle
	rc = acedEntSel("\nSelect Window entity ", ename, pickpt);
	if(rc != RTNORM)
	{
		acutPrintf("\nError selecting Window entity ");
		return;
	}

	// Get the entity object ID
	acdbGetObjectId(entId, ename);

	// Open the enity for a read operation
	acdbOpenObject(pObj, entId, AcDb::kForRead);

	pObj->getAcDbHandle(handEnt);

	handEnt.getIntoAsciiBuffer(strHandle);

    pObj->close();

	// See if the dictionary has an RectWindow object
	// for our entity.

	if(!pDict->has(strHandle))
	{
		acutPrintf("\nNot a RectWindow object");
		pDict->close();
		return;
	}

    pDict->getAt(strHandle, pObj, AcDb::kForRead);
    pDict->close();

	// Is pObj a RectWindow object ?
	pRectWindow = RectWindow::cast(pObj);
	if(!pRectWindow)
	{
		acutPrintf("\nObject in dictionary is not a RectWindow ");
		pObj->close();
		return;
	}

	acutPrintf("\nWindow Type = %s", pRectWindow->m_strWindType);
	acutPrintf("\nRows = %d", pRectWindow->getWindowRows());
	acutPrintf("\nCols = %d", pRectWindow->getWindowCols());
	acutPrintf("\nLength = %.2lf", pRectWindow->getWindowLength());
	acutPrintf("\nHeight = %.2lf", pRectWindow->getWindowHeight());
	acutPrintf("\nStart Point = (%.2lf, %.2lf)",
				pRectWindow->getWindowStartPoint().x,
				pRectWindow->getWindowStartPoint().y);

	pObj->close();
}

